<?php
namespace App\Console\Commands;

use Illuminate\Support\Facades\DB;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Redis;

class checkdatabase extends Command {

    /**
     * @var 设置执行的命令
     */
    protected $signature  = 'checkdatabase:send {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    // protected $description = 'test controller';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        // 获取参数，调用方法
        $action = $this->argument('action');
        $this->$action();

        //这里做任务的具体处理，可以用模型
        // Log::info('任务调度'.time());
    }

    public function new_database()
    {
        $tables = DB::select('show tables');

        // 这里数据库改正确的
        $tables = array_column($tables, 'Tables_in_ivy_dev');
        foreach ($tables as $key => $value) {
            $columns = Schema::getColumnListing($value);
            Redis::setex('new_'.$value, 3600, json_encode($columns));
        }
    }

    public function old_database()
    {
        $tables = DB::select('show tables');
        // 这里数据库改正确的
        $tables = array_column($tables, 'Tables_in_old_backend');
        foreach ($tables as $key => $value) {
            $columns = Schema::getColumnListing($value);
            Redis::setex('old_'.$value, 3600, json_encode($columns));
        }
    }

    /*
     * 查询新的库遍历
     * */
    public function check_database()
    {
        $tables = DB::select('show tables');
        // 这里数据库改正确的
        $tables = array_column($tables, 'Tables_in_backend');

        $cha_table = [];
        $cha_table_feild = [];
        foreach ($tables as $key => $value) {

            if(empty(Redis::get('old_'.$value))){
                $cha_table[] = $value;
                continue;
            }
            // 检测差别的字段
            $new_table_feild = json_decode(Redis::get('new_'.$value), true);
            $old_table_feild = json_decode(Redis::get('old_'.$value), true);
            foreach($new_table_feild as $v){
                if(!in_array($v, $old_table_feild)){
                    $cha_table_feild[$value][] = $v;
                }
            }
        }
        echo json_encode($cha_table);
        echo "<br />";
        echo json_encode($cha_table_feild);
        exit;
    }

    public function test(){
        echo 123;
    }

}